import axios from "axios";
import Vue from "vue";
import qs from "qs";
import { Loading } from "element-ui";

const request = axios.create({
  headers: {
    // 请求头
    // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    // 'X-Client-DeviceId': 'WebClient',
    // 'X-Client-DeviceType': 'WebClient',
    // 'X-Client-Version': '1.0.0.0'
    // 'X-Client-AgentId': 37
  },
  baseURL: "/",
  timeout: 605000
});
request.interceptors.request.use(
  // 请求设置
  function(config) {
    // console.log(config)
    if (config.method == "post") {
      if (config.url != "/api/userService/v1/webLogin") {
        const token = sessionStorage.getItem("token");
        if (config.data instanceof FormData) {
          config.data.append("token", token);
        } else {
          config.data.token = token;
        }
      }
    }

    return config;
  },
  function(error) {
    // console.log(err)
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
request.interceptors.response.use(
  // 响应设置
  function(response) {
    if (response.data.status == 10005 || response.data.status == 10004) {
      sessionStorage.removeItem("token");
      location.reload();
    }

    return response;
  },
  function(error) {
    alert(error);

    return Promise.reject(error);
  }
);

export default request;
